home *** CD-ROM | disk | FTP | other *** search
/ The Utilities Experience / The Utilities Experience - Volume 1.iso / software / misc / o-z / x-windows / gs262 / zfileio.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-29  |  11.1 KB  |  494 lines

  1. /* Copyright (C) 1989, 1992, 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* zfileio.c */
  20. /* File I/O operators for Ghostscript */
  21. #include "ghost.h"
  22. #include "gp.h"
  23. #include "errors.h"
  24. #include "oper.h"
  25. #include "stream.h"
  26. #include "estack.h"
  27. #include "files.h"
  28. #include "iscan.h"
  29. #include "store.h"
  30. #include "gsmatrix.h"            /* for gxdevice.h */
  31. #include "gxdevice.h"
  32. #include "gxdevmem.h"
  33.  
  34. /* Forward references */
  35. int zreadline_from(P4(byte *, uint, uint *, stream *));
  36. private es_ptr zget_current_file(P0());
  37. private int write_string(P2(os_ptr, stream *));
  38. #ifdef AMIGA
  39. extern int gs_writeppmfile(gx_device_memory *, FILE *);
  40. #endif
  41.  
  42. /* ------ Operators ------ */
  43.  
  44. /* <file> read <int> true */
  45. /* <file> read false */
  46. int
  47. zread(register os_ptr op)
  48. {    stream *s;
  49.     int ch;
  50.     check_read_file(s, op);
  51.     ch = sgetc(s);
  52.     if ( ch >= 0 )
  53.        {    make_int(op, ch);
  54.         push(1);
  55.         make_bool(op, 1);
  56.        }
  57.     else if ( ch == EOFC )
  58.         make_bool(op, 0);
  59.     else
  60.         return_error(e_ioerror);
  61.     return 0;
  62. }
  63.  
  64. /* <file> <int> write - */
  65. int
  66. zwrite(register os_ptr op)
  67. {    stream *s;
  68.     ulong ch;
  69.     check_write_file(s, op - 1);
  70.     check_type(*op, t_integer);
  71.     ch = op->value.intval;
  72.     if ( ch > 0xff )
  73.         return_error(e_rangecheck);
  74.     sputc(s, (byte)ch);
  75.     pop(2);
  76.     return 0;
  77. }
  78.  
  79. /* <file> <string> readhexstring <substring> <filled_bool> */
  80. int
  81. zreadhexstring(register os_ptr op)
  82. {    stream *s;
  83.     int code;
  84.     uint nread;
  85.     int odd = -1;
  86.     check_read_file(s, op - 1);
  87.     check_write_type(*op, t_string);
  88.     code = sreadhex(s, op->value.bytes, r_size(op), &nread, &odd, 1);
  89.     switch ( code )
  90.        {
  91.     case EOFC:
  92.         /* Reached end-of-file before filling the string. */
  93.         /* Return an appropriate substring. */
  94.         r_set_size(op, nread);
  95.         code = 1;
  96.         break;
  97.     case 0:
  98.         /* Filled the string. */
  99.         break;
  100.     default:            /* Error */
  101.         return_error(e_ioerror);
  102.        }
  103.     ref_assign(op - 1, op);
  104.     make_bool(op, 1 - code);
  105.     return 0;
  106. }
  107.  
  108. /* <file> <string> writehexstring - */
  109. int
  110. zwritehexstring(register os_ptr op)
  111. {    register stream *s;
  112.     register byte ch;
  113.     register const byte *p;
  114.     register const char _ds *hex_digits = "0123456789abcdef";
  115.     register uint len;
  116.     check_write_file(s, op - 1);
  117.     check_read_type(*op, t_string);
  118.     p = op->value.bytes;
  119.     len = r_size(op);
  120.     while ( len-- )
  121.        {    ch = *p++;
  122.         sputc(s, hex_digits[ch >> 4]);
  123.         sputc(s, hex_digits[ch & 0xf]);
  124.        }
  125.     pop(2);
  126.     return 0;
  127. }
  128.  
  129. /* <file> <string> readstring <substring> <filled_bool> */
  130. int
  131. zreadstring(register os_ptr op)
  132. {    stream *s;
  133.     uint len, rlen;
  134.     check_read_file(s, op - 1);
  135.     check_write_type(*op, t_string);
  136.     len = r_size(op);
  137.     rlen = sgets(s, op->value.bytes, len);
  138.     r_set_size(op, rlen);
  139.     op[-1] = *op;
  140.     make_bool(op, (rlen == len ? 1 : 0));
  141.     return 0;
  142. }
  143.  
  144. /* <file> <string> writestring - */
  145. int
  146. zwritestring(register os_ptr op)
  147. {    stream *s;
  148.     int code;
  149.     check_write_file(s, op - 1);
  150.     code = write_string(op, s);
  151.     if ( code >= 0 ) pop(2);
  152.     return code;
  153. }
  154.  
  155. /* <file> <string> readline <substring> <bool> */
  156. int
  157. zreadline(register os_ptr op)
  158. {    stream *s;
  159.     uint count;
  160.     int code;
  161.     check_read_file(s, op - 1);
  162.     check_write_type(*op, t_string);
  163.     code = zreadline_from(op->value.bytes, r_size(op), &count, s);
  164.     if ( code < 0 ) return code;
  165.     r_set_size(op, count);
  166.     op[-1] = *op;
  167.     make_bool(op, code);
  168.     return 0;
  169. }
  170.  
  171. /* Internal readline routine. */
  172. /* Returns 1 if OK, 0 if end of file, or an error code. */
  173. int
  174. zreadline_from(byte *ptr, uint size, uint *pcount, stream *s)
  175. {    uint count = 0;
  176.     int ch;
  177.     for ( ; ; count++ )
  178.        {
  179.         /* Most systems define \n as 0xa and \r as 0xd; however, */
  180.         /* OS-9 has \n == \r == 0xd and \l == 0xa.  The following */
  181.         /* code works properly regardless of environment. */
  182.  
  183.         switch ( ch = sgetc(s) )
  184.            {
  185.         case '\r':
  186. #if '\n' == '\r'            /* OS-9 or similar */
  187. #  define LF 0xa
  188.             if ( s != gs_stream_stdin )
  189.             {    ch = sgetc(s);
  190.                 if ( ch != LF && ch >= 0 )
  191.                     sputback(s);
  192.                         }
  193.             /* falls through */
  194.         case LF:
  195. #  undef LF
  196. #else                    /* '\n' != '\r' */
  197.             ch = sgetc(s);
  198.             if ( ch != '\n' && ch >= 0 ) sputback(s);
  199.             /* falls through */
  200.         case '\n':
  201. #endif
  202.             *pcount = count;
  203.             return 1;
  204.         case EOFC:
  205.             *pcount = count;
  206.             return 0;
  207.            }
  208.         if ( count >= size )    /* filled the string */
  209.            {    sputback(s);
  210.             return_error(e_rangecheck);
  211.            }
  212.         *ptr++ = ch;
  213.        }
  214.     /*return 0;*/        /* not reached */
  215. }
  216.  
  217. /* token - this is called from zstring.c */
  218. int
  219. ztoken_file(register os_ptr op)
  220. {    stream *s;
  221.     int code;
  222.     check_read_file(s, op);
  223.     switch ( code = scan_token(s, 0, (ref *)op) )
  224.        {
  225.     default:            /* possible error */
  226.         if ( code < 0 ) return code;
  227.                     /* read a token */
  228.         push(1);
  229.         make_true(op);
  230.         return 0;
  231.     case scan_EOF:            /* no tokens */
  232.         make_false(op);
  233.         return 0;
  234.        }
  235. }
  236.  
  237. /* <file> bytesavailable <int> */
  238. int
  239. zbytesavailable(register os_ptr op)
  240. {    stream *s;
  241.     long avail;
  242.     check_read_file(s, op);
  243.     if ( savailable(s, &avail) < 0 )
  244.         return_error(e_ioerror);
  245.     make_int(op, avail);
  246.     return 0;
  247. }
  248.  
  249. /* - flush - */
  250. int
  251. zflush(register os_ptr op)
  252. {    sflush(gs_stream_stdout);
  253.     return 0;
  254. }
  255.  
  256. /* <file> flushfile - */
  257. int
  258. zflushfile(register os_ptr op)
  259. {    stream *s;
  260.     check_file(s, op);
  261.     sflush(s);
  262.     pop(1);
  263.     return 0;
  264. }
  265.  
  266. /* <file> resetfile - */
  267. int
  268. zresetfile(register os_ptr op)
  269. {    NYI("resetfile");
  270.     pop(1);
  271.     return 0;
  272. }
  273.  
  274. /* - currentfile <file> */
  275. int
  276. zcurrentfile(register os_ptr op)
  277. {    es_ptr fp;
  278.     push(1);
  279.     /* Check the cache first */
  280.     if ( esfile != 0 )
  281.         ref_assign(op, esfile);
  282.     else if ( (fp = zget_current_file()) == 0 )
  283.        {    /* Return an invalid file object. */
  284.         /* This doesn't make a lot of sense to me, */
  285.         /* but it's what the PostScript manual specifies. */
  286. #ifndef AMIGA
  287.         make_file(op, 0, ~0, invalid_file_entry);
  288. #else
  289.         make_file(op, 0, (unsigned short)~0, invalid_file_entry);
  290. #endif
  291.        }
  292.     else
  293.        {    ref_assign(op, fp);
  294.         /* Load the cache */
  295.         esfile = fp;
  296.        }
  297.     /* Make the returned value literal. */
  298.     r_clear_attrs(op, a_executable);
  299.     return 0;
  300. }
  301.  
  302. /* <string> print - */
  303. int
  304. zprint(register os_ptr op)
  305. {    int code = write_string(op, gs_stream_stdout);
  306.     if ( code >= 0 ) pop(1);
  307.     return code;
  308. }
  309.  
  310. /* <bool> echo - */
  311. int
  312. zecho(register os_ptr op)
  313. {    check_type(*op, t_boolean);
  314.     /****** NOT IMPLEMENTED YET ******/
  315.     pop(1);
  316.     return 0;
  317. }
  318.  
  319. /* ------ Level 2 extensions ------ */
  320.  
  321. /* <file> fileposition <int> */
  322. int
  323. zfileposition(register os_ptr op)
  324. {    stream *s;
  325.     check_file(s, op);
  326.     if ( !sseekable(s) )
  327.         return_error(e_ioerror);
  328.     make_int(op, stell(s));
  329.     return 0;
  330. }
  331.  
  332. /* <file> <int> setfileposition - */
  333. int
  334. zsetfileposition(register os_ptr op)
  335. {    stream *s;
  336.     check_file(s, op - 1);
  337.     check_type(*op, t_integer);
  338.     if ( sseek(s, op->value.intval) < 0 )
  339.         return_error(e_ioerror);
  340.     pop(2);
  341.     return 0;
  342. }
  343.  
  344. /* ------ Ghostscript extensions ------ */
  345.  
  346. /* <file> <int> unread - */
  347. int
  348. zunread(register os_ptr op)
  349. {    stream *s;
  350.     ulong ch;
  351.     check_read_file(s, op - 1);
  352.     check_type(*op, t_integer);
  353.     ch = op->value.intval;
  354.     if ( ch > 0xff )
  355.         return_error(e_rangecheck);
  356.     if ( sungetc(s, (byte)ch) < 0 )
  357.         return_error(e_ioerror);
  358.     pop(2);
  359.     return 0;
  360. }
  361.  
  362. /* <file> <device> writeppmfile - */
  363. int
  364. zwriteppmfile(register os_ptr op)
  365. {    stream *s;
  366.     int code;
  367.     check_write_file(s, op - 1);
  368.     check_type(*op, t_device);
  369.     if ( !gs_device_is_memory(op->value.pdevice) )
  370.         return_error(e_typecheck);
  371.     sflush(s);
  372.     code = gs_writeppmfile((gx_device_memory *)(op->value.pdevice), s->file);
  373.     if ( code >= 0 ) pop(2);
  374.     return code;
  375. }
  376.  
  377. /* ------ Initialization procedure ------ */
  378.  
  379. op_def zfileio_op_defs[] = {
  380.     {"1bytesavailable", zbytesavailable},
  381.     {"0currentfile", zcurrentfile},
  382.     {"1echo", zecho},
  383.     {"1fileposition", zfileposition},
  384.     {"0flush", zflush},
  385.     {"1flushfile", zflushfile},
  386.     {"1print", zprint},
  387.     {"1read", zread},
  388.     {"2readhexstring", zreadhexstring},
  389.     {"2readline", zreadline},
  390.     {"2readstring", zreadstring},
  391.     {"1resetfile", zresetfile},
  392.     {"2setfileposition", zsetfileposition},
  393.     {"2unread", zunread},
  394.     {"2write", zwrite},
  395.     {"2writehexstring", zwritehexstring},
  396.     {"2writeppmfile", zwriteppmfile},
  397.     {"2writestring", zwritestring},
  398.     op_def_end(0)
  399. };
  400.  
  401. /* ------ Non-operator routines ------ */
  402.  
  403. /* Check a file for reading. */
  404. /* The interpreter calls this to check an executable file. */
  405. int
  406. file_check_read(ref *op, stream **ps)
  407. {    if ( !s_is_reading(*ps = fptr(op)) )
  408.         return_error(e_invalidaccess);
  409.     return 0;
  410. }
  411.  
  412. /* Get the current file from which the interpreter is reading. */
  413. private es_ptr
  414. zget_current_file(void)
  415. {    register es_ptr ep = esp;
  416.     while ( ep >= esbot )
  417.     {    if ( r_has_type_attrs(ep, t_file, a_executable) )
  418.             return ep;
  419.         ep--;
  420.     }
  421.     return (es_ptr)0;
  422. }
  423.  
  424. /* When closing a file, check whether it is the current file, */
  425. /* and if so, clear the cache. */
  426. void
  427. check_close_current_file(stream *s)
  428. {    /* If we just closed the file from which the interpreter */
  429.     /* is reading, zap it on the exec stack. */
  430.     es_ptr cfp = zget_current_file();
  431.     if ( cfp != 0 && fptr(cfp) == s )
  432.     {    /* A null would confuse the estack parser.... */
  433.         make_tasv(cfp, t_array, a_executable+a_execute, 0, refs, (ref *)0);
  434.         esfile = 0;        /* clear currentfile cache */
  435.     }
  436. }
  437.  
  438. /* Switch a file open for read/write access but currently in write mode */
  439. /* to read mode.  Return 1 if OK, 0 if not. */
  440. int
  441. file_switch_to_read(ref *op)
  442. {    stream *s = fptr(op);
  443.     uint modes = s->modes;
  444.     long pos;
  445.     if ( s->write_id != r_size(op) )    /* not valid */
  446.         return 0;
  447.     pos = stell(s);
  448.     if ( sflush(s) < 0 )
  449.         return 0;
  450.     s->read_id = s->write_id;        /* enable reading */
  451.     s->write_id = 0;            /* disable writing */
  452.     sread_file(s, s->file, s->cbuf, s->cbsize);
  453.     fseek(s->file, 0L, SEEK_CUR);        /* pacify C library */
  454.     s->modes |= modes & s_mode_append;    /* don't lose append info */
  455.     s->position = pos;
  456.     return 1;
  457. }
  458.  
  459. /* Switch a file open for read/write access but currently in read mode */
  460. /* to write mode.  Return 1 if OK, 0 if not. */
  461. int
  462. file_switch_to_write(ref *op)
  463. {    stream *s = fptr(op);
  464.     uint modes = s->modes;
  465.     long pos;
  466.     if ( s->read_id != r_size(op) )        /* not valid */
  467.         return 0;
  468.     pos = stell(s);
  469.     s->write_id = s->read_id;        /* enable writing */
  470.     s->read_id = 0;                /* disable reading */
  471.     fseek(s->file, pos, SEEK_SET);        /* pacify C library */
  472.     if ( modes & s_mode_append )
  473.         sappend_file(s, s->file, s->cbuf, s->cbsize);    /* sets position */
  474.     else
  475.     {    swrite_file(s, s->file, s->cbuf, s->cbsize);
  476.         s->position = pos;
  477.     }
  478.     return 1;
  479. }
  480.  
  481. /* ------ Internal routines ------ */
  482.  
  483. /* Write a string on a file.  The file has been checked for validity, */
  484. /* but not the string. */
  485. private int
  486. write_string(os_ptr op, stream *s)
  487. {    uint len;
  488.     check_read_type(*op, t_string);
  489.     len = r_size(op);
  490.     if ( sputs(s, op->value.bytes, len) != len )
  491.         return_error(e_ioerror);
  492.     return 0;
  493. }
  494.